package begin.view 
{
	import begin.BeginC;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	import interfaces.IOperate;
	import org.airmvc.Dispatcher;
	import ui.begin.ChooseMonarchUI;
	import utils.T;
	import vo.GeneralVO;
	
	/**
	 * 选择君主
	 * @author WLDragon 2014-03-03
	 */
	public class ChooseMonarch extends ChooseMonarchUI implements IOperate
	{
		
		public function ChooseMonarch() 
		{
			super();
			
			this.mouseEnabled = false;
			
			itemContainer = new Sprite();
			itemContainer.x = 50;
			itemContainer.y = 50;
			
			var xml:XML = T.db.getXml(Cnt.XML_MONARCH);
			var p:int = 0;
			for each(var m:XML in xml.monarch) 
			{
				var gvo:GeneralVO = T.db.getRecord(Cnt.VO_GENERAL, m.@generalId) as GeneralVO;
				var item:MonarchItem = new MonarchItem(gvo);
				item.index = p;
				item.name = m.@playerId;
				item.imgCastle.url = m.@castleUrl;
				item.btNumber.text = m.@castleNum;
				item.x = p % 2 * 227;
				item.y = (p * 0.5 >> 0) * 63;
				itemContainer.addChild(item);
				p++;
			}
			addChild(itemContainer);
			clpArrow.visible = true;
			Kyb.setOperater(this);
			
			//addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			//addEventListener(MouseEvent.CLICK,onClick);
		}
		
		private function onClick(e:MouseEvent):void 
		{
			var t:MonarchItem = e.target as MonarchItem;
			if (t)
			{
				Dispatcher.send(BeginC.CHOOSE_MONARCH,this,int(curItem.name));//选择参数，发送指令
			}
		}
		
		private function onMouseOver(e:MouseEvent):void 
		{
			var m:MonarchItem = e.target as MonarchItem;
			if (m)
			{
				index = m.index;
			}
		}
		
		public function execute(keyCode:int) : void
		{
			switch (keyCode) 
			{
				case Keyboard.A:
					index--;
				break;
				case Keyboard.D:
					index++;
				break;
				case Keyboard.W:
					index -= 2;
				break;
				case Keyboard.S:
					index += 2;
				break;
				case Keyboard.SPACE:
					Dispatcher.send(BeginC.CHOOSE_MONARCH,this,int(curItem.name));
				break;
			}
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			if (value < 0 || value >= itemContainer.numChildren || value == _index)
				return;
				
			curItem = itemContainer.getChildAt(value) as MonarchItem;
			clpArrow.x = itemContainer.x + curItem.x - 16;
			clpArrow.y = itemContainer.y + curItem.y + 8;
			_index = value;
			
			Dispatcher.send(BeginC.CHECK_MONARCH,this,curItem.data);
		}
		
		private var itemContainer:Sprite;
		private var curItem:MonarchItem;
		private var _index:int = -1;
		
	}

}